﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.houseWork.Helper;

namespace CIT.WMS.output
{
    public partial class FrmOutputShelf : Form
    {
        bool flag = false;
        bool result = false;

        public FrmOutputShelf()
        {
            InitializeComponent();
            checklgs = Utils.CheckConnectLGS(out ip, out port);
        }
        bool checklgs = false;
        string port = "";
        string ip = "";
        string Location_Type = string.Empty;//库位类型1：普通库位；2：智能库位
        BarcodeUtils obj = new BarcodeUtils();

        private void txt_kuwei_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (CheckLocation())
                {
                    Utils.SetTsLab(lab_msg, "[" + txt_kuwei.Text + "]校验合法.", new List<string> { "OK" }, Color.Lime);
                    txt_reelid.SelectAll();
                    txt_reelid.Focus();
                    Utils.uContext.Accountk__BackingField = "mes40";
                }
                else
                {
                    txt_kuwei.SelectAll();
                }
            }
        }
        /// <summary>
        /// 校验库位
        /// </summary>
        /// <returns></returns>
        private bool CheckLocation()
        {
            if (txt_kuwei.Text.Trim() != "0")
            {
                #region 智能货架校验
                DataTable dt1 = Utils.NMS.QueryDataTable(Utils.uContext, @" select A.SerialNumber,B.Enable_Flag,B.Location_Type,B.MainBoardId,B.PositionId FROM dbo.T_Bllb_StorageLocation_tbsl B 
 LEFT join  dbo.T_Bllb_StockInfo_tbsi A  ON A.Location_SN=b.Location_SN
 WHERE B.Location_SN=@Location_SN",
                    new CIT.WMS.MESWcf.CmdParameter[] { new CIT.WMS.MESWcf.CmdParameter() { ParameterName = "@Location_SN", Value = txt_kuwei.Text } });
                if (dt1.Rows.Count > 0)
                {
                    //库位是否有料
                    if (SqlInput.ChangeNullToString(dt1.Rows[0]["SerialNumber"]).Length == 0)
                    {
                        Utils.SetTsLab(lab_msg, "[" + txt_kuwei.Text + "]未绑定物料", new List<string> { "Fail" }, Color.Red);
                        txt_kuwei.SelectAll();
                        txt_kuwei.Focus();
                        return false;
                    }
                    //库位是否禁用
                    if (SqlInput.ChangeNullToString(dt1.Rows[0]["Enable_Flag"]) != "Y")
                    {
                        //lab_msg.Text = "[" + txt_kuwei.Text + "]已禁用";
                        //lab_msg.ForeColor = Color.Red;
                        Utils.SetTsLab(lab_msg, "[" + txt_kuwei.Text + "]已禁用", new List<string> { "Fail" }, Color.Red);
                        txt_kuwei.SelectAll();
                        txt_kuwei.Focus();
                        return false;
                    }
                    if (SqlInput.ChangeNullToString(dt1.Rows[0]["Location_Type"]) == "2")
                    {
                        Utils.LightOrder(new List<StorageLight>() { new StorageLight() { ContinuedTime = 10, lightOrder = 2, MainBoardId = int.Parse(dt1.Rows[0]["MainBoardId"].ToString()), RackPositionId = dt1.Rows[0]["PositionId"].ToString() } }, checklgs, ip, port);
                    }
                }
                #endregion
            }
            return true;
        }
        private void txt_reelid_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (!CheckLocation())
                {
                    return;
                }
                string strSql = string.Empty;
                strSql = string.Format(@" select A.Storage_SN, A.Location_SN,A.SerialNumber,A.Enable,A.Lock_Flag FROM  dbo.T_Bllb_StockInfo_tbsi A  where A.SerialNumber='{0}'", txt_reelid.Text.Trim());
                DataTable dt_ReelId = Utils.NMS.QueryDataTable(Utils.uContext, strSql, new CIT.WMS.MESWcf.CmdParameter[0]);
                if (SqlInput.ChangeNullToString(dt_ReelId.Rows[0]["Lock_Flag"]) != "0")
                {
                    Utils.SetTsLab(lab_msg, "ReelID状态不对,不允许下架", new List<string> { "Fail" }, Color.Red);
                    txt_reelid.SelectAll();
                    txt_reelid.Focus();
                    return;
                }
                if (dt_ReelId.Rows.Count == 0)
                {
                    Utils.SetTsLab(lab_msg, "物料不仓库内", new List<string> { "Fail" }, Color.Red);
                    return;
                }
                else
                {
                    string locationSN = SqlInput.ChangeNullToString(dt_ReelId.Rows[0]["Location_SN"]);
                    if (locationSN == string.Empty)//未上架
                    {
                        Utils.SetTsLab(lab_msg, "物料SN尚未上架.", new List<string> { "Fail" }, Color.Red);
                        txt_reelid.SelectAll();
                        txt_reelid.Focus();
                        return;
                    }
                    if (locationSN != txt_kuwei.Text.Trim())
                    {
                        Utils.SetTsLab(lab_msg, "物料SN不在当前库位，在库位" + locationSN + "上", new List<string> { "Fail" }, Color.Red);
                        txt_reelid.SelectAll();
                        txt_reelid.Focus();
                        return;
                    }
                }
                strSql = string.Format(@"UPDATE T_Bllb_StockInfo_tbsi SET Location_SN='',Area_SN='' WHERE SerialNumber='{0}'", txt_reelid.Text.Trim());
                Utils.NMS.Exectransql(Utils.uContext, strSql, new CIT.WMS.MESWcf.CmdParameter[0], out flag, out result);
                Utils.SetTsLab(lab_msg, "物料SN下架[" + txt_kuwei.Text + "]成功.", new List<string> { "OK" }, Color.Lime);
                txt_kuwei.Text = "";
                txt_reelid.Text = "";
                txt_kuwei.SelectAll();
                txt_kuwei.Focus();
                return;
            }
        }

        private void btn_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_yes_Click(object sender, EventArgs e)
        {

        }
    }
}